import { SearchArticle, SearchHistory, Suggestion } from '@/types/data'
import { SearchAction } from '@/types/store'
import { gethistory } from '@/utils/storagehistory'

type SearchState = {
  suggestion: Suggestion
  searchHistory: SearchHistory
    // 存放搜索的结果
    results: SearchArticle
}

const initialState: SearchState = {
  suggestion: [],
  searchHistory: gethistory(), // 初始搜索关键字
  results:[]
}

const Search = (state = initialState, action: SearchAction): SearchState => {
  switch (action.type) {
    case 'search/suggestion':
      return {
        ...state,
        suggestion: action.payload,
      }
    case 'search/history' :
      return {...state,searchHistory:action.payload}
    case 'search/clear' :
      return {...state,searchHistory:[]}
    case 'search/result':
      return {...state,results:action.payload}
    default:
      return state
  }
}

export default Search